/*
 * @FileName: [PosServiceImpl.java] 
 * @Package com.bcinfo.iccard.service.impl 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev: 789 $
 * $LastChangedDate: 2013-12-25 12:59:24 +0800 (Wed, 25 Dec 2013) $
 * $LastChangedBy: wanggc $
 * 
 * @category ICCARD
 * @version 1.1
 * @author $Author: wanggc $
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bcinfo.iccard.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bcinfo.iccard.dao.MerchantMapper;
import com.bcinfo.iccard.dao.PosMapper;
import com.bcinfo.iccard.dao.SerialNumberMapper;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.model.Merchant;
import com.bcinfo.iccard.model.Pos;
import com.bcinfo.iccard.model.SerialNumber;
import com.bcinfo.iccard.service.PosService;

/**
 * PosService实现类 <br/>
 * <p>Description:<br/>
 *    <p>TODO 提供了pos数据的新增、修改、删除、按商家及pos编号查询等方法
 *
 * <p>For Examples <br/>
 * <PRE>
 * </PRE>
 * <p>
 */
@Service
public class PosServiceImpl implements PosService
{
    @Autowired
    private PosMapper _posMapper ;
    @Autowired
    private MerchantMapper _merchantMapper;
    @Autowired
    private SerialNumberMapper _serialNumberMapper;
    private static final Logger _logger=Logger.getLogger(PosServiceImpl.class);
    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param posId
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.PosService#load(long)
     */
    @Override
    public Pos load(long posId) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("load:posId="+posId);
        }
        try
        {
            Pos pos = _posMapper.selectByPrimaryKey(posId);
            return pos;
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param posCode
     * @param merchantCode
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.PosService#loadByCode(java.lang.String, java.lang.String)
     */
    @Override
    public Pos loadByCode(String posCode, String merchantCode) throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("loadByCode:posCode="+posCode+",merchantCode="+merchantCode);
        }
        try
        {
            Pos pos = _posMapper.loadByCode(posCode,merchantCode);
            return pos;
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param pos
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.PosService#add(com.bcinfo.iccard.model.Pos)
     */
    @Override
    public Pos add(Pos pos) throws IccardDatabaseException
    {
        if (pos == null)
        {
            _logger.error("parameter pos is null,please check it again!");
            return null;
        }
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:act=" + pos.toString());
        }
        try
        {
            _posMapper.insertSelective(pos);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
        return pos;
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param pos
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.PosService#update(com.bcinfo.iccard.model.Pos)
     */
    @Override
    public int update(Pos pos) throws IccardDatabaseException
    {
        if (pos == null)
        {
            _logger.error("parameter pos is null,please check it again!");
            return 0;
        }
        if (_logger.isDebugEnabled())
        {
            _logger.debug("update:pos=" + pos.toString());
        }
        int rows = 0;
        try
        {
            rows = _posMapper.updateByPrimaryKeySelective(pos);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
        return rows;
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param merchantId
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.PosService#deleteByMerchant(long)
     */
    @Override
    public int deleteByMerchant(long merchantId) throws IccardDatabaseException
    {

        if (_logger.isDebugEnabled())
        {
            _logger.debug("deleteByMerchant:merchantId=" + merchantId);
        }
        int rows = 0;
        try
        {
            rows = _posMapper.deleteByMerchant(merchantId);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
        return rows;
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param merchantEncode
     * @return 
     * @see com.bcinfo.iccard.service.PosService#loadByMerchantEncode(java.lang.String)
     */
    @Override
    public Merchant loadByMerchantEncode(String merchantEncode) throws IccardDatabaseException
    {
        if (merchantEncode == null)
        {
            _logger.error("parameter merchantEncode is null,please check it again!");
            return null;
        }
        if (_logger.isDebugEnabled())
        {
            _logger.debug("loadByMerchantEncode:merchantEncode=" + merchantEncode);
        }
        try
        {
            Merchant m=_merchantMapper.selectByEnCode(merchantEncode);
            return m;
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new IccardDatabaseException();
        }
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @return
     * @throws IccardDatabaseException 
     * @see com.bcinfo.iccard.service.PosService#generateSerialNum()
     */
    @Override
    public String generateSerialNum() throws IccardDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("generateSerialNum");
        }
        SerialNumber sn=new SerialNumber();
        _serialNumberMapper.insert(sn);
        if(sn.getSerialNumber()!=null){
            String serialNum=StringUtils.leftPad(String.valueOf(sn.getSerialNumber()), 12, '0');
            if(_logger.isDebugEnabled()){
                _logger.debug("serialNum:"+serialNum);
            }
            return serialNum;
        }
        return null;
    }

}
